const SDK = {

  data:{
    serveType: "local",
    sdkName: "",
    baseSDKPath: "",
    sdkFilePath: "",
    iosSdkName: "",
    baseiosSDKPath: "",
    sdkiosFilePath: "",
  },
  methods: {
    createSdk(type){
      if(type == 'Android'){
        showDialog({
          title:'安卓sdk路径',
          content:` 
          <div id='form'>
            <div>打包安卓sdk路径(检查是否在对应目录下面)</div>
            <div class="form-item">
              <label>选择默认路径: </label> 
              <div >
                <select name="sdkDir"  > 
                  <option value="" >自定义</option>
                  <option value="C\:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Android\\\\Sdk" >C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk</option>
                  <option value="/usr/local/sdk" >/usr/local/sdk</option>
                  <option value="/Users/apple/Documents/systemTools/sdk" >/Users/apple/Documents/systemTools/sdk</option>
                </select>
              </div>
            </div>
            <div class="form-item">
              <label>自定义输入路径: </label> 
              <input  type="text"  name='sdkDir' placeholder="请输入${type}-sdk路径" />
            </div>
            
          </div>
          `, 
          confirm:()=>{
            const form = document.getElementById('form');
            const inputs = form.querySelectorAll('input');
            const data = {};
            for(let i = 0;i<inputs.length;i++){
              if(inputs[i].value == '') return alert(inputs[i].placeholder)
              data[inputs[i].name] = inputs[i].value
            }
            this.createSdkUp(type,data.sdkDir)
            return message({type:'success',message:'开始上传sdk'})
          }
        })
      }else{
        this.createSdkUp(type)
         message({type:'success',message:'开始上传sdk'})

      }

    },
    createSdkUp(type,sdkDir) {
      let path = ''
      if(this.sdkData.serveType == 'local'){
        if(type == 'Android'){
          if(!this.sdkData.baseSDKPath ) return alert('请输入安卓sdk路径')
          if(!this.sdkData.sdkFilePath) return alert('请选择sdk文件')
          if(this.sdkData.sdkFilePath.indexOf("Android-SDK@") == -1) return alert('安卓SDK文件名字必须包含Android-SDK@')
          path = this.sdkData.baseSDKPath+this.sdkData.sdkFilePath;
    
        }else{
          if(!this.sdkData.baseiosSDKPath ) return alert('请输入iossdk路径')
          if(!this.sdkData.sdkiosFilePath) return alert('请选择iossdk文件')
          console.log(this.sdkData.sdkiosFilePath.indexOf("Ios-SDK@"))
          if(this.sdkData.sdkiosFilePath.indexOf("Ios-SDK@") == -1) return alert('IosSDK文件名字必须包含Ios-SDK@')
          path = this.sdkData.baseiosSDKPath+this.sdkData.sdkiosFilePath;
    
        }
        // 通知服务器粘贴sdk
        fetchPost('/sdk', {path,type,sdkDir}).then(res => {
          // 刷新浏览器
          window.location.reload();
        })
      }else{
        let data = null
        if(type == 'Android'){
          if(!this.sdkData.sdkFilePath) return alert('请选择sdk文件')
          data = this.sdkData.sdkFilePath
        }else{
          if(!this.sdkData.sdkiosFilePath) return alert('请选择iossdk文件')
          console.log(this.sdkData.sdkiosFilePath)
          data = this.sdkData.sdkiosFilePath
        }
        this.uploadSDK(data,type,sdkDir)
      }
    },
    // 上传sdk
    uploadSdkSelect(e,name) {
      let file = e.target.files[0];
      if(file.name.toUpperCase().indexOf('SDK') == -1) return alert('请选择sdk文件')
      if(this.sdkData.serveType == 'local'){
        let xg = navigator.platform.indexOf("Win") == 0 ? "\\" : "/";
        this.sdkData[name] = xg+file.name;
      }else{
        this.sdkData[name] = file;

      }
    },
    
  },
  

}

// js 事件代理监听动态创建的file input

window.addEventListener('change',function(e){
  if(e.target.type == 'select-one' && e.target.name == 'sdkDir'){
    const value = e.target.value;
    const input = document.querySelector('input[name="sdkDir"]');
    input.value = value;
  }
})
    